//
//  BCConversionCurrencyHeaderView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/7.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCConversionCurrencyHeaderView.h"

//
#import "BConversionCurrencyButton.h"

//
#import "BCConversionResponse.h"

static CGFloat BCConversionCurrencyHeaderViewTopPadding = 13.0;
static CGFloat BCConversionCurrencyHeaderViewLabeladding = 10.0;

@interface BCConversionCurrencyHeaderView ()

@property (nonatomic, strong) QMUILabel *conversionCaptionLabel;

/**
 *conversionLabel
 */
@property (nonatomic, strong) QMUILabel *conversionLabel;


/**
 *BConversionCurrencyButton
 */
@property (nonatomic, strong) BConversionCurrencyButton *currencyButton;

@end

@implementation BCConversionCurrencyHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        //
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.conversionCaptionLabel = [QMUILabel new];
    self.conversionCaptionLabel.font = [self.class captionFont];
    self.conversionCaptionLabel.textColor = [UIColor qmui_colorWithHexString:@"#9B9B9B"];
    [self.contentView addSubview:self.conversionCaptionLabel];
    //
    
    self.conversionLabel = [QMUILabel new];
    self.conversionLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.conversionLabel.font = [self.class conversionFont];
    self.conversionLabel.adjustsFontSizeToFitWidth = true;
    [self.contentView addSubview:self.conversionLabel];
    
    self.currencyButton = [BConversionCurrencyButton new];
    [self.contentView addSubview:self.currencyButton];
    
    
    //
    [self.conversionCaptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(BCConversionCurrencyHeaderViewTopPadding);
        make.left.mas_equalTo(self.contentView).offset(15.0);
        make.height.mas_equalTo([self.class captionHeight]);
    }];
    [self.conversionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.conversionCaptionLabel.mas_bottom).offset(BCConversionCurrencyHeaderViewLabeladding);
        make.left.mas_equalTo(self.conversionCaptionLabel);
        make.height.mas_equalTo([self.class conversionHeight]);
        make.right.mas_equalTo(self.contentView).offset(-15.0);
    }];
    
    //
    [self.currencyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.conversionCaptionLabel);
        make.right.mas_equalTo(self.contentView).offset(-6.0);
    }];
    
    //
    [self.currencyButton addTarget:self action:@selector(currencyButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setConversionData:(BCConversionData *)conversionData {
    _conversionData = conversionData;
    
    //
    self.conversionCaptionLabel.text = BCLocalizedKey(@"Conversion Currency");
    //
    BCConversionDataTickerModel *ticker = conversionData.currentTicker;
    if (ticker) {
        NSInteger count = conversionData.ticker.count;
        //
        self.currencyButton.needMoreArrorw = count>1?true:false;
        self.currencyButton.currencySign = ticker.sign;
        
        //计算兑换
        NSString *conversionAmount = conversionData.conversionAmount;
        //
        NSString *text = @"--";
        if (conversionAmount.length) {
            CGFloat amount = conversionAmount.floatValue;
            CGFloat exchangeAmount = amount*ticker.last;
            //
            text = [NSString stringWithFormat:@"%@",@(exchangeAmount)];
        }
        //
        self.conversionLabel.text = text;
    }
}

#pragma mark - currencyButtonDidClickedAction
- (void)currencyButtonDidClickedAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(conversionCurrencyHeaderViewCurrencyButtonDidClickedAction)]) {
        [self.delegate conversionCurrencyHeaderViewCurrencyButtonDidClickedAction];
    }
}


+ (UIFont *)captionFont {
    return [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
}
+ (UIFont *)conversionFont {
    return [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
}
+ (CGFloat)captionHeight {
    return [self captionFont].lineHeight+1;
}
+ (CGFloat)conversionHeight {
    return [self conversionFont].lineHeight+1;
}

+ (CGFloat)defaultHeight {
    CGFloat height = 0;
    height += BCConversionCurrencyHeaderViewTopPadding;
    height += [self captionHeight];
    height += BCConversionCurrencyHeaderViewLabeladding;
    height += [self conversionHeight];
    //
    height += 20.0;
    return height;
}

@end
